rm(list = ls())
set.seed(123)
### This creates the figures in the main paper

# Load Packages
library(tidyverse) # 1.3.1
library(stargazer) # 5.2.2
library(scales) # 1.1.1
library(modelsummary) # 0.7.0
library(vtable) # 1.3.1
library(gtsummary) # 1.4.1
library(gridExtra) # 2.3
library(jtools) # 2.1.3
library(sandwich) # 3.0-1


### Load data
data <- read.csv("perspective_taking_merged.csv")

## Filter out those who refused informed consent 
## and failed attention checks
data %>% 
  filter(informed_consent == 1 & attention_check1 == 3) %>%
  mutate_all(na_if,"") %>%
  filter(age >= 18) %>%
  filter(!is.na(attention_check1)) -> data

################################################
################ Figure 1 ######################
################################################

data %>% 
  mutate(treatment = factor(treatment, 
                            levels = c("Control Undoc",
                                       "Treat Undoc",
                                       "Cancer",
                                       "Immigrant",
                                       "Control Low Inc",
                                       "Treat Low Inc")),
         order = recode(treatment, # Create ordering label
                        "Control Low Inc" = 1,
                        "Treat Low Inc" = 2,
                        "Control Undoc" = 3,
                        "Treat Undoc" = 4,
                        "Cancer" = 5,
                        "Immigrant" = 6),
         groups = recode(treatment, # Label target group
                         "Control Undoc" = "Undocumented",
                         "Treat Undoc" = "Undocumented",
                         "Cancer" = "Undocumented",
                         "Immigrant" = "Immigrant",
                         "Control Low Inc" = "Low Income",
                         "Treat Low Inc" = "Low Income")) %>%
  group_by(study, treatment, order, treated, groups) %>%
  summarise(n = n(),
            mean = mean(response_deserving, na.rm = T),
            se = sd(response_deserving) / sqrt(n())) %>%
  filter(!is.na(treatment)) %>%
  ggplot(aes(x = reorder(treatment, -order), y = mean,
             shape = as.factor(study), 
             color = as.factor(study),
             ymin = mean - (2*se),
             ymax = mean + (2*se))) +
  scale_x_discrete(labels = c("Control Undoc" = "Control",
                              "Treat Undoc" = "Perspective Taking\n+ COVID-19",
                              "Cancer" = "Perspective Taking\n+ Cancer",
                              "Immigrant" = "Perspective Taking\n+ COVID-19",
                              "Control Low Inc" = "Control",
                              "Treat Low Inc" = "Perspective Taking\n+ COVID-19") ) +
  geom_pointrange(position = position_dodge2(w = 0.5)) +
  theme_bw() +
  scale_y_continuous(labels = scales::percent) +
  facet_grid(groups ~ .,
             labeller = as_labeller(c("Undocumented" = "Undocumented\nImmigrant",
                                      "Immigrant" = "Immigrant",
                                      "Low Income" = "Low Income\nIndividual")),
             scales = "free_y") + 
  coord_flip() +
  scale_shape_discrete(name = "",
                       labels = c("Study 1",
                                  "Study 2")) +
  scale_color_manual(name = "",
                     labels = c("Study 1",
                                "Study 2"),
                     values = c("dark blue", "red3") ) +
  theme(legend.position = "top",
        axis.text.x = element_text(size = rel(1.25)),
        axis.text.y = element_text(size = rel(1.45)),
        strip.text = element_text(size = 10)) +
  labs(x = "", y = "Mean Deservingness Support", title = "")
ggsave("figure1_FINAL.jpeg")


################################################
################ Figure 2 ######################
################################################

data %>%
  mutate(order = recode(treatment,
                        "Control Undoc" = 1,
                        "Treat Undoc" = 2,
                        "Cancer" = 3,
                        "Immigrant" = 4,
                        "Control Low Inc" = 5,
                        "Treat Low Inc" = 6)) %>%
  group_by(pid_3level, treatment, treated, order) %>%
  mutate(treatment = factor(treatment,
                            levels = c("Control Undoc",
                                       "Treat Undoc",
                                       "Cancer",
                                       "Immigrant",
                                       "Control Low Inc",
                                       "Treat Low Inc"))) %>%
  summarise(n = n(),
            mean = mean(response_deserving, na.rm = T),
            se = sd(response_deserving) / sqrt(n())) %>%
  filter(!is.na(pid_3level),
         !is.na(treatment)) %>%
  ggplot(aes(x = reorder(treatment, -order), y = mean,
             ymax = (mean + 2*se), 
             ymin = (mean - 2*se),
             color = as.factor(treated),
             shape = as.factor(treated)) ) +
  geom_pointrange(position = position_dodge2(w = 0.5)) +
  coord_flip() +
  theme_bw() +
  scale_y_continuous(labels = scales::percent,
                     breaks = c(0,.10,.30,.50,.70,.90)) +
  facet_wrap(pid_3level ~.,
             labeller = as_labeller(c(`1` = "Democrats",
                                      `2` = "Independents",
                                      `3` = "Republicans")),
             scales = "free_x") +
  scale_x_discrete(labels = c("Control Undoc" = "Control \n+ Undocumented",
                              "Treat Undoc" = "Undocumented \n+ COVID-19",
                              "Cancer" = "Undocumented \n+ Cancer",
                              "Immigrant" = "Immigrant \n+ COVID-19",
                              "Control Low Inc" = "Control \n+ Low Income",
                              "Treat Low Inc" = "Low Income \n+ COVID-19") ) +
  scale_color_manual(name = "",
                     labels = c("Control",
                                "Perspective-Taking"),
                     values = c("dark blue",
                                "red3")) +
  scale_shape_discrete(name = "",
                       labels = c("Control",
                                  "Perspective-Taking")) +
  theme(legend.position = "top",
        axis.text.x = element_text(size = rel(1.45) ),
        axis.text.y = element_text(size = rel(1.45)),
        strip.text = element_text(size = 10))  +
  labs(x = "", y = "Mean Deservingness Support", title = "")
ggsave("figure2_FINAL.jpeg")


################################################
################ Figure 3 ######################
################################################

########## Part A ###################
data %>%
  filter(study == 1 &
           treatment %in% c("Control Undoc",
                            "Treat Undoc")) %>%
  mutate(treatment2 = factor(treatment, 
                             levels = c("Treat Undoc",
                                        "Control Undoc")),
         treated = ifelse(treatment == "Control Undoc",
                          "Control", "Perspective Taking")) %>%
  group_by(treatment2, treated) %>%
  summarise(n = n(),
            mean = mean(response_policy, na.rm = T),
            se = sd(response_policy, na.rm =T)/sqrt(n())) %>%
  ggplot(aes(x = treatment2, y = mean,
             ymax = mean + (2*se), 
             ymin = mean - (2*se),
             shape = treated, color = treated)) +
  geom_pointrange(position = position_dodge(w = 0.75)) +
  labs(x = "", y = "Mean Policy Support") +
  theme_bw() +
  theme(legend.position = "top",
        axis.text.x = element_text(size=rel(1.75)),
        axis.text.y = element_text(size = rel(1.5)),
        legend.text = element_text(size = 14)) +
  scale_color_manual(name = "",
                     values = c("dark blue", 
                                "red3"),
                     labels = c("Control", 
                                "Perspective-Taking\nUndocumented\nCOVID-19")) +
  scale_shape_discrete(name = "",
                       labels = c("Control", 
                                  "Perspective-Taking\nUndocumented\nCOVID-19")) +
  scale_y_continuous(labels = scales::percent, 
                     limits = c(.42, .58)) +
  scale_x_discrete(labels = c("Treat Undoc" = "Undocumented \n+ COVID-19", 
                              "Control Undoc" = "Control") ) +
  coord_flip()
ggsave("figure3_partA_FINAL.jpeg")


########## Part B ###################

data %>%
  filter(study == 2) %>% # select study 2
  mutate(treatment_s2 = factor(treatment_s2,
                               levels = c("Welfare",
                                          "Health",
                                          "Health & Welfare")),
         treatment2 = factor(treatment, 
                             levels = c("Control Undoc",
                                        "Treat Undoc",
                                        "Cancer",
                                        "Immigrant"))) %>%
  group_by(treatment_s2, treatment2) %>%
  summarise(n = n(),
            mean = mean(response_policy, na.rm = T),
            se = sd(response_policy, na.rm = T) / sqrt(n()) ) %>% 
  filter(!is.na(treatment_s2),
         !is.na(treatment2)) %>%
  ggplot(aes(x = treatment_s2, 
             y = mean, 
             ymax = mean + (2*se),
             ymin = mean - (2*se),
             shape = as.factor(treatment2),
             color = as.factor(treatment2)) ) +
  geom_pointrange(position = position_dodge2(w = .5)) +
  theme_bw() + 
  facet_grid(treatment_s2 ~ . , scales = "free") + 
  scale_y_continuous(labels = scales::percent) +
  scale_color_manual(name = "",
                     labels = c("Control",
                                "Perspective-Taking\nUndocumented\nCOVID-19",
                                "Perspective-Taking\nUndocumented\nCancer",
                                "Perspective-Taking\nImmigrant\nCOVID-19"),
                     values = c("dark blue",
                                "red3",
                                "firebrick3",
                                "firebrick4")) +
  scale_shape_discrete(name = "",
                       labels = c("Control",
                                  "Perspective-Taking\nUndocumented\nCOVID-19",
                                  "Perspective-Taking\nUndocumented\nCancer",
                                  "Perspective-Taking\nImmigrant\nCOVID-19")) +
  coord_flip() +
  theme(legend.position = "top") +
  labs(x = "", y = "Mean Policy Support", title = "")
ggsave("figure3_partB_FINAL.jpeg")


################################################
################ Figure 4 ######################
################################################

data %>% 
  filter(treatment %in% c("Control Undoc", # Select groups with undocumented immigrants
                          "Treat Undoc")) %>% 
  mutate(treated = factor(treated, # Label treated groups
                          levels = c("Treated",
                                     "Control")),
         covid_ever = ifelse(covid_contracted == 1 | # label personal lived experience
                               covid_self == 1, "Yes", "No")) %>%
  group_by(covid_ever, treated) %>%
  summarise(n = n(),
            mean = mean(response_deserving, na.rm = T),
            se = sd(response_deserving) / sqrt(n())) %>%
  filter(!is.na(covid_ever)) %>%
  ggplot(aes(x = treated, y = mean,
             ymax = (mean + 2*se), 
             ymin = (mean - 2*se),
             shape = as.factor(covid_ever), 
             color = as.factor(covid_ever)) ) +
  geom_pointrange(position = position_dodge2(w = 0.5)) +
  coord_flip() +
  theme_bw() +
  scale_y_continuous(labels = scales::percent) +
  scale_x_discrete(labels = c("Control" = "Control", 
                              "Treated" = "Perspective Taking") ) +
  theme_bw() +
  scale_color_manual(name = "COVID-19\nLived Experience",
                     labels = c("No",
                                "Yes"),
                     values = c("dark blue", "red3")) +
  scale_shape_discrete(name = "COVID-19\nLived Experience",
                       labels = c("No",
                                  "Yes")) +
  theme(legend.position = "top") +
  labs(x = "", y = "Mean Deservingness Support", title = "")
ggsave("figure4_FINAL.jpeg")


